package com.xintoucloud.pbs.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xintoucloud.pbs.dao.DevelopMemberDao;
import com.xintoucloud.pbs.entity.DevelopMember;
import com.xintoucloud.pbs.model.DmDto;
import com.xintoucloud.pbs.service.DevelopMemberService;
import com.xintoucloud.pbs.service.DmStageService;
import com.xintoucloud.pbs.service.OrganizationService;
@Service@Transactional
public class DevelopMemberServiceImpl implements DevelopMemberService{
	@Autowired
    private DevelopMemberDao developMemberDao;
	@Autowired
	private DmStageService dmStageService;
	@Autowired
	private OrganizationService organizationService;
	
	@Override
	public boolean save(DmDto dmd) {
		DevelopMember thedm = new DevelopMember();
		thedm.setBid(dmd.getBid());
		thedm.setDmnum(dmd.getIdnum());
		thedm.setName(dmd.getName());
		thedm.setNation(dmd.getNation());
		thedm.setSex(dmd.getSex());
		thedm.setIdnum(dmd.getIdnum());
		thedm.setEducation(dmd.getEducation());
		thedm.setWorkUnit(dmd.getWorkUnit());
		thedm.setWorkJob(dmd.getWorkJob());
		thedm.setWorkJobType(dmd.getWorkJobType());
		thedm.setFamilyAddress(dmd.getFamilyAddress());
		thedm.setPic(dmd.getPic());
		thedm.setDms(dmStageService.getOne(dmd.getDmsid()));
		thedm.setOrg(organizationService.getOne(dmd.getOid()));
		return developMemberDao.save(thedm)!=null;
	}

	@Override
	public void delete(Integer bid) {
		developMemberDao.delete(bid);
	}

	@Override
	public void update(DmDto dmd) {
		DevelopMember thedm = developMemberDao.getOne(dmd.getBid());
		thedm.setBid(dmd.getBid());
		thedm.setDmnum(dmd.getDmnum());
		thedm.setName(dmd.getName());
		thedm.setNation(dmd.getNation());
		thedm.setSex(dmd.getSex());
		thedm.setIdnum(dmd.getIdnum());
		thedm.setEducation(dmd.getEducation());
		thedm.setWorkUnit(dmd.getWorkUnit());
		thedm.setWorkJob(dmd.getWorkJob());
		thedm.setWorkJobType(dmd.getWorkJobType());
		thedm.setFamilyAddress(dmd.getFamilyAddress());
		thedm.setPic(dmd.getPic());
		thedm.setDms(dmStageService.getOne(dmd.getDmsid()));
		thedm.setOrg(organizationService.getOne(dmd.getOid()));
	}

	@Override@Transactional(readOnly=true)
	public DevelopMember getOne(Integer bid) {
		return developMemberDao.getOne(bid);
	}

	@Override@Transactional(readOnly=true)
	public List<DevelopMember> list(Integer oid) {	
		return developMemberDao.list(oid);
	}

	@Override@Transactional(readOnly=true)
	public List<DmDto> list2(Integer oid) {
		List<DevelopMember> dmlist = developMemberDao.list(oid);
		List<DmDto> dmdlist = new ArrayList<>();
		
		for(DevelopMember dm : dmlist) {
			DmDto dmd = new DmDto();
			dmd.setBid(dm.getBid());
			dmd.setDmnum(dm.getDmnum());
			dmd.setName(dm.getName());
			dmd.setSex(dm.getSex());
			dmd.setNation(dm.getNation());
			dmd.setIdnum(dm.getIdnum());
			dmd.setEducation(dm.getEducation());
			dmd.setWorkUnit(dm.getWorkUnit());
			dmd.setWorkJob(dm.getWorkJob());
			dmd.setWorkJobType(dm.getWorkJobType());
			dmd.setFamilyAddress(dm.getFamilyAddress());
			dmd.setPic(dm.getPic());
			dmd.setOid(dm.getOrg().getOid());
			dmd.setOname(dm.getOrg().getOname());
			dmd.setDmsid(dm.getDms().getDmsid());
			dmd.setStageName(dm.getDms().getStageName());
			//获取该发展党员下一阶段id值
			int nextStageId = dm.getDms().getDmsid()+1;
			//通过id取得下一阶段的DmStage对象 取得其stageName为DmDto对象赋值
			dmd.setNextStageName(dmStageService.getOne(nextStageId).getStageName());
			dmdlist.add(dmd);
		}
		return dmdlist;
	}

}
